<?php
class M_bookings extends CI_Model
{

	public function getTimes(){
		$q = $this->db->order_by("aps_time_from", "asc")->get('appointment_schedules');
//		die($this->db->last_query());
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function getBookedTimes($stylistid, $appointmentDate){
		$q = $this->db->query("SELECT time_id FROM appointments WHERE stylistid = '$stylistid' AND appointment_date = '$appointmentDate'");
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function getTime($id){
		$q = $this->db->get_where('appointment_schedules', array('aps_id' => $id));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function addTime($data){
		$data['added'] = NOW;
		$this->db->insert('appointment_schedules', $data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function deleteTime($id) {
		$this->db->delete('appointment_schedules', array('aps_id' => $id));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function updateTime($timeData, $timeId){
		$timeData['updated'] = NOW;
		$this->db->where('aps_id', $timeId)->update('appointment_schedules', $timeData);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	
	}
	
	public function setStatus($data, $id)
	{
		$data['updated'] = NOW;
		$this->db->where('id', $id)->update('appointments',$data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function addAppointment($data){
		$data['created'] = NOW;
		$data['updated'] = NOW;
		$data['status'] = 'Pending';
		$this->db->insert('appointments', $data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	
	public function getAllBookings(){
		$query = "SELECT a.id, a.service, a.status, a.appointment_date as adate, time_id, customer_note, admin_note, service, s.firstname as sname, s.lastname as slname, c.firstname as cfname, c.lastname as clname, aps_time_from, aps_time_to FROM appointments a LEFT JOIN users s ON s.id = a.stylistid LEFT JOIN users c ON c.id = a.userid LEFT JOIN appointment_schedules aps ON aps.aps_id = a.time_id";	
		$q = $this->db->query($query);
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function getStylistBookings($stylist_id){
		$query = "SELECT a.id, a.userid, a.service, a.status, a.appointment_date as adate, time_id, customer_note, admin_note, service, s.firstname as sname, s.lastname as slname, c.firstname as cfname, c.lastname as clname, aps_time_from, aps_time_to FROM appointments a LEFT JOIN users s ON s.id = a.stylistid LEFT JOIN users c ON c.id = a.userid LEFT JOIN appointment_schedules aps ON aps.aps_id = a.time_id WHERE stylistid = '$stylist_id' AND status = 'Confirmed' ";	
		$q = $this->db->query($query);
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function getBooking($booking_id){
		$query = "SELECT a.id, a.service, a.status, a.created, a.appointment_date as adate, time_id, customer_note, admin_note, service, s.firstname as sname, s.lastname as slname, c.firstname as cfname, c.lastname as clname, c.contact as ccontact, c.email as cemail, aps_time_from, aps_time_to FROM appointments a LEFT JOIN users s ON s.id = a.stylistid LEFT JOIN users c ON c.id = a.userid LEFT JOIN appointment_schedules aps ON aps.aps_id = a.time_id WHERE a.id = '$booking_id'";
		$q = $this->db->query($query);
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function getCustomerAppointments($userid){
		$q = $this->db->query("SELECT appointments.id as id, appointments.stylistid as s_id, appointments.userid, status, appointment_date,service, u.firstname as sfname, u.lastname as slname, time_id, aps_time_from, aps_time_to FROM appointments LEFT JOIN users u ON u.id = appointments.stylistid LEFT JOIN appointment_schedules aps ON aps.aps_id = appointments.time_id WHERE appointments.userid = '$userid'");
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function deleteBooking($id) {
		$this->db->delete('appointments', array('id' => $id));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
}

?>